package org.i_plus.model.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.i_plus.model.BlogBean;
import org.i_plus.model.Blog_ReplyBean;
import org.i_plus.model.Blog_Reply_Interface;
import org.i_plus.model.MemberBean;
import org.i_plus.model.HibernateUtil.HibernateUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Blog_ReplyHibernateDAO implements Blog_Reply_Interface {
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private static final String SELECT_ALL = "from Blog_ReplyBean order by B_num";

	@Override
	public Blog_ReplyBean select(int Re_B_num) {

		Session session = sessionFactory.getCurrentSession();
		Blog_ReplyBean bean = (Blog_ReplyBean) session.get(Blog_ReplyBean.class, Re_B_num);
		return bean;
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//
//		try {
//			session.beginTransaction();
//			blog_ReplyBean = (Blog_ReplyBean) session.get(Blog_ReplyBean.class,
//					id);
//
//			session.getTransaction().commit();
//
//		} catch (RuntimeException e) {
//			session.getTransaction().rollback();
//			throw e;
//		}
//
//		return blog_ReplyBean;
	}

	public List<Blog_ReplyBean> selectByBNum(int b_num){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Blog_ReplyBean where BlogBean.B_num=:b_num");
		query.setInteger("b_num", b_num);
		return  (List<Blog_ReplyBean>)query.list();
	}
	@Override
	public List select() {
		List list = null;

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		try {
			session.beginTransaction();
			Query query = session.createQuery(SELECT_ALL);

			list = query.list();

			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			throw e;
		}

		return list;
	}
	public List<Blog_ReplyBean> selectByacc(String id){
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery("from Blog_ReplyBean where Re_B_Id=:id");
		query.setString("id", id);
		return (List<Blog_ReplyBean>) query.list();
	}

	@Override
	public void insertBlog_Reply(int b_num,String Re_B_Id,String Re_B_content,Timestamp Re_B_Time) {
		Session session = sessionFactory.getCurrentSession();
		Blog_ReplyBean bean = new Blog_ReplyBean();
		BlogBean blog = new BlogBean();
		MemberBean member = new MemberBean();
		blog.setB_num(b_num);
		bean.setBlogBean(blog);
		member.setAccount(Re_B_Id);
		bean.setMemberBean(member);
		bean.setRe_B_content(Re_B_content);
		bean.setRe_B_Time(Re_B_Time);
		session.save(bean);
		
		
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//
//		try {
//			session.beginTransaction();
//			session.save(bean);
//			session.getTransaction().commit();
//		} catch (RuntimeException e) {
//			session.getTransaction().rollback();
//			throw e;
//		}

	}

	@Override
	public void update(int b_num,int re_b_num,String Re_B_Id,String Re_B_content,Timestamp Re_B_Time) {

		Session session = sessionFactory.getCurrentSession();
		Blog_ReplyBean bean = (Blog_ReplyBean) session.get(Blog_ReplyBean.class, re_b_num);
		BlogBean blog = new BlogBean();
		MemberBean member = new MemberBean();
		blog.setB_num(b_num);
		bean.setBlogBean(blog);
		bean.setRe_B_content(Re_B_content);
		member.setAccount(Re_B_Id);
		bean.setMemberBean(member);
		bean.setRe_B_Time(Re_B_Time);
		session.saveOrUpdate(bean);
		
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//
//		try {
//			session.beginTransaction();
//			session.update(bean);
//			session.getTransaction().commit();
//		} catch (RuntimeException e) {
//			session.getTransaction().rollback();
//			throw e;
//		}
	}

	@Override
	public void delete(int re_B_num) {
		
		Session session = sessionFactory.getCurrentSession();
		Blog_ReplyBean bean =(Blog_ReplyBean) session.get(Blog_ReplyBean.class, re_B_num);
		System.out.println("bean:"+bean);
		session.delete(bean);
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//
//		try {
//			session.beginTransaction();
//
//			Blog_ReplyBean br = (Blog_ReplyBean) session.get(
//					Blog_ReplyBean.class, re_B_num);
//			System.out.println(br.getRe_B_Id());
//			session.delete(br);
//			session.getTransaction().commit();
//		} catch (RuntimeException e) {
//			session.getTransaction().rollback();
//			throw e;
//		}

	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		SessionFactory session=(SessionFactory) context.getBean("sessionFactory");
		Transaction tx = session.getCurrentSession().beginTransaction();
		Blog_ReplyHibernateDAO dao= (Blog_ReplyHibernateDAO) context.getBean("blog_ReplyDAO");
		
		List<Blog_ReplyBean> lists=dao.selectByBNum(18);
		for(Blog_ReplyBean temp:lists)
		System.out.println(temp.getRe_B_content());
//		dao.insertBlog_Reply(18, "456", "qwer", Timestamp.valueOf("2014-02-23 00:00:00"));
		
//		Blog_ReplyBean bean = dao.select(4);
//		System.out.println(bean.getBlogBean().getB_num());
//		System.out.println(bean.getRe_B_num());
//		System.out.println(bean.getRe_B_Time());
//		System.out.println(bean.getRe_B_content());
		
		
		
		
//		Blog_ReplyBean bean =new Blog_ReplyBean();
//		bean.setRe_B_content("zxcv");
//		bean.setRe_B_num(2);
//		bean.setB_num(5);
//		bean.setRe_B_Id("456");
//		bean.setRe_B_Time(Timestamp.valueOf("2014-02-23 00:00:00"));
//		dao.update(18,15,"123","zzzz",Timestamp.valueOf("2014-02-23 00:00:00"));
		
//		dao.delete(36);
		
		
		
		
		
//		Blog_ReplyBean bean = new Blog_ReplyBean();
//		 bean.setB_num(1);
//		bean.setRe_B_num(19);
//		bean.setRe_B_Id("BB");
//		bean.setRe_B_content("oo");
//		bean.setRe_B_Time(Timestamp.valueOf("2014-07-09 00:00:00"));
//		dao.insertBlog_Reply(bean);
		// System.out.println("getRe_B_content"+bean.getRe_B_content());
		// System.out.println("getRe_B_Id"+bean.getRe_B_Id());
		// System.out.println("getRe_B_num"+bean.getRe_B_num());
		// System.out.println("getB_num"+bean.getB_num());
		// System.out.println("getRe_B_Time"+bean.getRe_B_Time());
		// //看有沒有執行到這，並把它抓出來顯示。
		//
		System.out.println("done");

		// Blog_ReplyBean bean1 = new Blog_ReplyBean();
		// bean1.setB_num(1);
		// bean1.setRe_B_num(14);
		// bean1.setRe_B_Id("BB");
		// bean1.setRe_B_content("so");
		// bean1.setRe_B_Time(Timestamp.valueOf("1991-01-01 23:23:00"));
		// dao.update(bean1);
		// System.out.println("done");

		// dao.delete(11);
		// System.out.println("done");

		// Blog_ReplyBean bean2 = dao.select(14);
		// System.out.println(bean2.getB_num() + ",");
		// System.out.println(bean2.getRe_B_num() + ",");
		// System.out.println(bean2.getRe_B_Id() + ",");
		// System.out.println(bean2.getRe_B_content() + ",");
		// System.out.println(bean2.getRe_B_Time());
		// System.out.println("done");

		// List<Blog_ReplyBean> list = dao.select();
		// System.out.println(list);
		// for (Blog_ReplyBean alist : list) {
		// System.out.println(alist.getB_num() + ",");
		// System.out.println(alist.getRe_B_num() + ",");
		// System.out.println(alist.getRe_B_Id() + ",");
		// System.out.println(alist.getRe_B_content() + ",");
		// System.out.println(alist.getRe_B_Time());
		// System.out.println("done");
		//
		// }
		tx.commit();
	}

}
